package com.matete.agentmanage.service;


import com.matete.agentmanage.model.ReturnRoom;
import org.apache.commons.lang3.StringUtils;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.matete.agentmanage.common.GlobalSettings;
import com.matete.agentmanage.model.Agent;
import com.matete.agentmanage.model.Room;
import com.matete.agentmanage.model.User;

import java.util.Map;

public class RoomService{
	private static final Room roomDao = new Room().dao();
	private static final ReturnRoom returnRoomDao = new ReturnRoom().dao();
	/**
	 * 根据ID查询会议室的信息
	 * 
	 * @param ID
	 * @return
	 */
	public Room getRoom(String ID) {
		return roomDao.findById(ID);
	}
	
	public static final Agent agentDao = new Agent().dao();

	/**
	 * 获取正在使用的会议室列表
	 * 
	 * @param pageNumber
	 *            分页当前页码
	 * @param parentId
	 *            上级代理ID
	 * @param searchContent
	 *            搜索内容
	 * @return
	 */
	public Page<Room> getRoomRecord(int pageNumber, int parentId, String searchContent) {
		String selectSQL = "SELECT *";
		//String selectSQL = "select a.ID,a.Name,a.Phone,a.QQ,a.Password,a.AgentCount,a.TotalHouseCardCount,a.CurHouseCardCount,a.ParentId,a.State,a.MaxAgentCount,a.AgentCount,a.CreateTime,a.LoginStatus, p.PropertyName AgentLevel ";
		if (StringUtils.isNotBlank(searchContent)) {
			Page<Room> room = null;
			/*room = roomDao.paginate(pageNumber, GlobalSettings.PAGE_SIZE, selectSQL,
					"from agent a , property p where p.PropertyKey =? and a.AgentLevel = p.PropertyValue AND State = ? and a.ID = ?",
					GlobalSettings.PROPERTY_KEY_AGENTLEVEL, GlobalSettings.AGENT_STATE_AUDITING, searchContent);*/
			
			room = roomDao.paginate(pageNumber, GlobalSettings.PAGE_SIZE, selectSQL,"from room ,where Status = ?",GlobalSettings.ROOM_STATE_VIA);
			if (room.getList().size() == 0) {
				room = roomDao.paginate(pageNumber, GlobalSettings.PAGE_SIZE, selectSQL,
						"from agent a , property p where p.PropertyKey =? and a.AgentLevel = p.PropertyValue AND State = ?  and a.Name like ?",
						GlobalSettings.PROPERTY_KEY_AGENTLEVEL, GlobalSettings.AGENT_STATE_AUDITING,
						"%" + searchContent + "%");
			}
			return room;
		}
		return roomDao.paginate(pageNumber, GlobalSettings.PAGE_SIZE, selectSQL,"FROM room where Status= ?",GlobalSettings.ROOM_STATE_VIA);
	}
	
	/**
	 * 查询目前会议室列表
	 * 
	 * @param pageNumber
	 *            分页当前页码
	 * @param searchContent
	 *            搜索内容
	 * @return
	 */
	public Page<Room> getRoomAuditingList(int pageNumber, String searchContent) {
		String selectSQL = "SELECT *";
		if (StringUtils.isNotBlank(searchContent)) {
			Page<Room> room = null;
			room = roomDao.paginate(pageNumber, GlobalSettings.PAGE_SIZE, selectSQL,"from room");
			if (room.getList().size() == 0) {
				room = roomDao.paginate(pageNumber, GlobalSettings.PAGE_SIZE, selectSQL,
						"from agent a , property p where p.PropertyKey =? and a.AgentLevel = p.PropertyValue AND State = ?  and a.Name like ?",
						GlobalSettings.PROPERTY_KEY_AGENTLEVEL, GlobalSettings.AGENT_STATE_AUDITING,
						"%" + searchContent + "%");
			}
			return room;
		}
		return roomDao.paginate(pageNumber, GlobalSettings.PAGE_SIZE, selectSQL,"FROM room");
	}
	
	/**
	 * 审核通过
	 * 
	 * @param ID
	 */
	public void agentAuditingVia(String ID) {
		roomDao.findById(ID).setStatus(GlobalSettings.AGENT_STATE_VIA).update();
	}

	public void delRoom(String ID) {
		String sql = "UPDATE room SET UserName = ' ' ,Phone = ' ' ,Date = ' ' ,StartTime = ' ' , EndTime = ' ' WHERE ID = ? ";
		Db.update(sql, ID);
	}

    /**
     * 删除该ID的房间
     * @param id
     */
    public void deleteRoom(String id) {
        Room room = roomDao.findById(id);
        room.delete();
    }


	/**
	 * 将传入的map存储到 return_room 表中
	 * @param map
	 * @return
	 */
	public void saveReturnRoom(Map<String, String> map,User user) {
        ReturnRoom returnRoom = new ReturnRoom();
        returnRoom.setRoomID(Integer.valueOf(user.getRoomID()));
        returnRoom.setStartTime(user.getStartTime());
        returnRoom.setEndTime(user.getEndTime());
        returnRoom.setUrl(map.get("url"));
        returnRoom.setPhone(user.getPhone());
        returnRoom.setUserName(user.getUserName());
        returnRoom.setRoomName(roomDao.findById(user.getRoomID()).getRoomName());
        returnRoom.save();
        return;
	}

    /**
     * 获取已经归还的会议室分页
     * @param pageNumber
     * @param searchContent
     * @return
     */
    public Page<ReturnRoom> getReturnRoomRecord(int pageNumber, String searchContent) {
        String selectSQL = "SELECT *";
        return returnRoomDao.paginate(pageNumber, GlobalSettings.PAGE_SIZE, selectSQL,"FROM return_room");
    }
}